from time import time

import pyglet
from pyglet.gl import *

class HUD (object):
    def __init__(self, window, level, ball):
        self.window = window
        self.level = level
        self.ball = ball

        self.last_score = self.ball.score
        self.score = pyglet.text.Label(text = '%05d' % self.last_score, font_name = 'DejaVuSans', font_size = 24, 
                x = 5, y = 10, color = (0, 0, 0, 255))
        self.time = pyglet.text.Label(text = '0:00', font_name = 'DejaVuSans', font_size = 24, 
                x = self.window.width - 5, y = 5, color = (0, 0, 0, 255,), anchor_x = 'right')

        self.started = self.level.started = time()
        
    def render(self):
        glMatrixMode(GL_PROJECTION)
        glPushMatrix()
        glLoadIdentity()
        gluOrtho2D(0, self.window.width, 0, self.window.height)
        glMatrixMode(GL_MODELVIEW)
        glPushMatrix()
        glLoadIdentity()

        if self.ball.score > self.last_score:
            self.last_score += max(1, (self.ball.score - self.last_score) / 10.)
            self.score.text = '%05d' % self.last_score

        elapsed = time() - self.started
        m, s = elapsed / 60, elapsed % 60
        self.time.text = '%d:%02d' % (m, s)

        self.score.draw()
        self.time.draw()

        glBegin(GL_LINES)
        glColor3f(0, 0, 0)
        for i in range(self.ball.power):
            x1 = i * 36 + 5
            x2 = x1 + 32
            glVertex2f(x1, 5)
            glVertex2f(x2, 5)

        glEnd()

        glPopMatrix()
        glMatrixMode(GL_PROJECTION)
        glPopMatrix()
        glMatrixMode(GL_MODELVIEW)
